//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  NotificationChannelListLeadTime.cs
//    
// Abstract:
//
//  A list of notification channels with the reminder time expressed as a relative lead time.
//  
//--

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Microsoft.EnhancedReminders
{
    /// <summary>
    /// The list of notification channels and lead time for using the channels.
    /// </summary>
    [Serializable()]
    public sealed class NotificationChannelListLeadTime : NotificationChannelList
    {
        /// <summary>
        /// Value used when the lead time is set to "none".
        /// </summary>
        public const int None = -1;

        /// <summary>
        /// Reminder time in minutes before the associated event.
        /// </summary>
        private int _leadTime = None;

        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="channelList">Channel list to copy</param>
        public NotificationChannelListLeadTime(NotificationChannelListLeadTime channelList)
            : base(channelList)
        {
            _leadTime = channelList._leadTime;
        }

        /// <summary>
        /// Ctor.  Populates the list of channels from a given (master) list of channels.
        /// </summary>
        /// <param name="channels">The master list of channels to initialize from.</param>
        public NotificationChannelListLeadTime(IEnumerable<NotificationChannel> channels)
            : base(channels)
        {
        }

        /// <summary>
        /// Ctor.  Populates the list of channels from a given (master) list of channels and lead time.
        /// </summary>
        /// <param name="channels">The master list of channels to initialize from.</param>
        /// <param name="leadTime">The lead time.</param>
        public NotificationChannelListLeadTime(IEnumerable<NotificationChannel> channels, int leadTime)
            : base(channels)
        {
            _leadTime = leadTime;
        }

        /// <summary>
        /// Creates a copy of a channel list instance.
        /// </summary>
        /// <returns></returns>
        public override NotificationChannelList Copy()
        {
            return new NotificationChannelListLeadTime(Channels, LeadTime);
        }

        /// <summary>
        /// The lead time, in minutes, prior to the notification event.
        /// </summary>
        public int LeadTime
        {
            get
            {
                return _leadTime;
            }
            set
            {
                _leadTime = value;
            }
        }

        /// <summary>
        /// Indicates if this channel list is enabled.
        /// </summary>
        public override bool IsEnabled
        {
            get
            {
                return _leadTime != None && base.IsEnabled;
            }
        }

        /// <summary>
        /// Determines if the specified channel list is equivalent to this channel list.
        /// </summary>
        /// <param name="channelList">Channel list to compare against</param>
        /// <remarks>
        /// This method verifies that the channel lists have the same enabled channel references and in the same order.
        /// It also verifies that the lead times are the same.
        /// </remarks>
        /// <returns>
        /// True if the channel lists are equivalent; false otherwise
        /// </returns>
        public override bool Equals(NotificationChannelList channelList)
        {
            if (!base.Equals(channelList))
            {
                return false;
            }

            NotificationChannelListLeadTime channelListLeadTime = channelList as NotificationChannelListLeadTime;

            // Check that the specified channel list is of the correct type
            if (channelListLeadTime == null)
            {
                return false;
            }

            return _leadTime == channelListLeadTime._leadTime;
        }
    }
}
